﻿using LibraryManagement.Custom_Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LibraryManagement.Security
{
    public partial class LoginForm : libForm
    {
        private LibManagementEntities context;
        public LoginForm()
        {
            InitializeComponent();
            context = new LibManagementEntities();
        }

        private void libTableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            context = new LibManagementEntities();
            if (txtUsername.Text.Trim() != "")
            {
                SecAccountDetail OSecAccountDetail = context.SecAccountDetails.Single(x => x.LoginId == txtUsername.Text);
                if (txtPwd.Text == clsEncyrption.AES.Decrypt(OSecAccountDetail.Password, "tristana#18"))
                {
                    clsStatics.currentUserId = OSecAccountDetail.AccountId;
                    ShowParentForm();
                }
                else
                    MessageBox.Show("Password was incorrect");
            }
            else
                MessageBox.Show("Login Id was incorrect.");
        }

        private void lnlCreateAccount_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            NewAccount frmNewAccount = new NewAccount();
            frmNewAccount.ShowDialog();
        }

        private void btnByPass_Click(object sender, EventArgs e)
        {
            clsStatics.currentUserId = 1;
            ShowParentForm();
        }

        private void ShowParentForm()
        {
            LibraryManagement.ParentForm frmParentForm = new ParentForm(this);
            frmParentForm.Show();
            this.Hide();
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            clsUpdate.UpdateDatabase();
            try
            {
                clsStatics.currentActiveSessionId = context.Sessions.Single(x => x.ActiveSession == true).SessionId;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
